package com.geeklan.smart.data.remote;
import com.geeklan.smart.data.remote.device.DeviceApiService;
import com.geeklan.smart.data.remote.common.CommonApiService;
import com.geeklan.smart.data.remote.pet.PetApiService;
import com.geeklan.smart.data.remote.user.UserApiService;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.util.concurrent.TimeUnit;

public class RetrofitClient {

    private static RetrofitClient instance;
    private Retrofit retrofit;

    private RetrofitClient() {
        // 配置 OkHttpClient
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .addInterceptor(new TokenInterceptor())
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .build();

        // 配置 Retrofit
        retrofit = new Retrofit.Builder()
                .baseUrl(NetworkConstant.BASE_URL+"/app/")
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    // 单例模式获取 RetrofitClient 实例
    public static synchronized RetrofitClient getInstance() {
        if (instance == null) {
            instance = new RetrofitClient();
        }
        return instance;
    }

    public ApiService getApiService() {
        return retrofit.create(ApiService.class);
    }

    public DeviceApiService getCmdApiService() {
        return retrofit.create(DeviceApiService.class);
    }

    public UserApiService getUserApiService() {
        return retrofit.create(UserApiService.class);
    }
    public PetApiService getPetApiService() {
        return retrofit.create(PetApiService.class);
    }
    public CommonApiService getMsgApiService() {
        return retrofit.create(CommonApiService.class);
    }
}
